﻿using Mall.Repository.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Mall.Repository.RepositoryProgram
{
    internal class NewExpressionToSql : BaseExpressionToSql<NewExpression>
    {
        protected override SqlBuilder Where(NewExpression expression, SqlBuilder sqlBuilder)
        {
            return base.Where(expression, sqlBuilder);
        }

        protected override SqlBuilder Insert(NewExpression expression, SqlBuilder sqlBuilder)
        {
            string text = " (";
            string text2 = " values (";
            for (int i = 0; i < expression.Members.Count; i++)
            {
                MemberInfo memberInfo = expression.Members[i];
                text = text + memberInfo.Name + ",";
                ConstantExpression constantExpression = expression.Arguments[i] as ConstantExpression;
                string text3 = sqlBuilder.AddDbParameter(constantExpression.Value, allowAutoAppend: false);
                text2 = text2 + text3 + ",";
            }

            if (text[text.Length - 1] == ',')
            {
                text = text.Remove(text.Length - 1, 1);
            }

            text += ")";
            if (text2[text2.Length - 1] == ',')
            {
                text2 = text2.Remove(text2.Length - 1, 1);
            }

            text2 += ")";
            sqlBuilder += text + text2;
            return sqlBuilder;
        }

        protected override SqlBuilder Update(NewExpression expression, SqlBuilder sqlBuilder)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                MemberInfo memberInfo = expression.Members[i];
                ConstantExpression constantExpression = expression.Arguments[i] as ConstantExpression;
                sqlBuilder += memberInfo.Name + " =";
                sqlBuilder.AddDbParameter(constantExpression.Value);
                sqlBuilder += ",";
            }

            if (sqlBuilder[sqlBuilder.Length - 1] == ',')
            {
                sqlBuilder.Remove(sqlBuilder.Length - 1, 1);
            }

            return sqlBuilder;
        }

        protected override SqlBuilder Select(NewExpression expression, SqlBuilder sqlBuilder)
        {
            foreach (Expression argument in expression.Arguments)
            {
                ExpressionToSqlProvider.Select(argument, sqlBuilder);
            }

            foreach (MemberInfo member in expression.Members)
            {
                sqlBuilder.SelectFieldsAlias.Add(member.Name);
            }

            return sqlBuilder;
        }

        protected override SqlBuilder GroupBy(NewExpression expression, SqlBuilder sqlBuilder)
        {
            foreach (Expression argument in expression.Arguments)
            {
                ExpressionToSqlProvider.GroupBy(argument, sqlBuilder);
            }

            return sqlBuilder;
        }

        protected override SqlBuilder OrderBy(NewExpression expression, SqlBuilder sqlBuilder)
        {
            foreach (Expression argument in expression.Arguments)
            {
                ExpressionToSqlProvider.OrderBy(argument, sqlBuilder);
            }

            return sqlBuilder;
        }
    }
}
